/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.interpreter.value;


import java.util.LinkedHashMap;
import java.util.Map;

import dex.compiler.checker.type.MemberType;
import dex.compiler.checker.type.Type;
import dex.compiler.checker.type.TypeManager;


/**
 * A value of a member type.
 */
public class MemberValue extends ReferenceValue {


	/**
	 * Maps field name to field type.
	 */
	final private Map<String,Type> types;


	/**
	 * Maps field name to field value.
	 */
	final private Map<String,Value> values;

	
	/**
	 * The member type of this member value.
	 */
	final private Type type;


	/**
	 * Constructs a new member value.
	 * 
	 * @param manager   used to look up appropriate field types
	 * @param t   the type of the new value
	 */
	public MemberValue(TypeManager manager, MemberType t) {
		this.type = t;
		this.types = manager.fields(manager.getDefinition(t));
		this.values = new LinkedHashMap<String,Value>(types.size());
		for (Map.Entry<String,Type> e: types.entrySet()) {
			Value v = Defaults.get(e.getValue());
			values.put(e.getKey(), v);
		}
	}

	
	/**
	 * Returns the fields of this member value.
	 * The returned map maps field name to field value.
	 * Note that the returned map guarantees to iterate
	 * over the fields in the order they were declared.
	 * 
	 * @return  a map from field names to field values
	 */
	public Map<String,Value> fields() {
		return values;
	}


	@Override
	public Type type() {
		return type;
	}
	
	
	@Override
	public String toString() {
		return values.toString();
	}

}
